package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Index;

/**
 * This entity represents instances of given TrainingTypeEntityImpl for given related PersonEntityImpl.
 * This means that given PersonEntityImpl has passed given TrainingTypeEntityImpl.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="TRAINING")
public class TrainingEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = -1120811474121576871L;

	/*
	 * Entity attributes
	 */
	@Column(nullable=false)
	@Index(name="IDX_TRAININGS_LAST_PASS")
	private Date passTimestamp;

	@Column(nullable=true)
	private Date evidencePassExpiration;

	@ManyToOne(fetch = FetchType.EAGER, targetEntity=TrainingTypeEntityImpl.class, optional=false) // no cascade
	private TrainingTypeEntityImpl trainingType;

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.EAGER, targetEntity = PersonEntityImpl.class, mappedBy = "trainings") // no cascade!
	private Set<PersonEntityImpl> persons;

	public TrainingEntityImpl()
	{
		super();
	}

	public TrainingEntityImpl(TrainingTypeEntityImpl trainingType, Date evidencePassExpiration, Date passTimestamp)
	{
		super();
		this.setTrainingType(trainingType);
		this.setEvidencePassExpiration(evidencePassExpiration);
		this.setPassTimestamp(passTimestamp);
	}

	public TrainingTypeEntityImpl getTrainingType()
	{
		return trainingType;
	}

	
	public void setTrainingType(TrainingTypeEntityImpl trainingType)
	{
		this.trainingType = trainingType;
	}

	
	public Date getEvidencePassExpiration()
	{
		return evidencePassExpiration;
	}

	
	public void setEvidencePassExpiration(Date evidencePassExpiration)
	{
		this.evidencePassExpiration = evidencePassExpiration;
	}

	public Date getPassTimestamp()
	{
		return passTimestamp;
	}

	public Set<PersonEntityImpl> getPersons()
	{
		return persons;
	}

	public List<PersonEntityImpl> getPersonsAsList()
	{
		return new ArrayList<PersonEntityImpl>(getPersons());
	}

	public void setPassTimestamp(Date passTimestamp)
	{
		this.passTimestamp = passTimestamp;
	}

	
  public void setPersons(Set<PersonEntityImpl> persons)
  {
    this.persons = persons;
  }

  @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("TrainingEntityImpl [getTrainingType()=");
		builder.append(getTrainingType());
		builder.append(", getEvidencePassExpiration()=");
		builder.append(getEvidencePassExpiration());
		builder.append(", getPassTimestamp()=");
		builder.append(getPassTimestamp());
		builder.append("]");
		return builder.toString();
	}

	/**
	 * Can be deleted (no relation).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}
